<?php

namespace App\Api\Controllers;

use App\Models\Cart;
use App\Models\OrderGoods;
use App\Models\WebConfig;
use Dingo\Api\Http\Request;
use Dingo\Api\Routing\Helpers;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;

class CartController extends Controller
{
    use Helpers;

    //购物车列表
    public function index(){
        $user=$this->auth->user();
        $result=DB::table('cart as c')
            ->leftJoin('goods as g','c.goods_id','=','g.id')
            ->where([['c.user_id',$user->id],['c.status',0]])
            ->select('c.id','c.goods_id','c.goods_num','c.selected','g.title','g.publisher','g.author','g.price','g.img','g.discount')
            ->get();
        if(count($result)>0){
            $extraDiscount=(int)WebConfig::where('name','extra_discount')->value('value')?:0;
            $totalMoney=0;
            foreach($result as $v){
                $v->price=(float)$v->price;
                if($v->selected){
                    $totalMoney+=$v->price*($v->discount+$extraDiscount)/100;
                }
            }
            return $this->response()->array([
                'result'=>['totalMoney'=>$totalMoney,'cartList'=>$result,'extraDiscount'=>$extraDiscount],
                'status_code' => 200,
                'message' => '获取成功'
            ]);
        }else{
            return $this->response()->array([
                'status_code' => 201,
                'message' => '购物车空空如也'
            ]);
        }


    }

    /**
     * @param Request $request
     * @return mixed
     * 加入购物车
     * 当前只支持单个数量的购买，利于后端匹配
     * 可重复购买
     */
    public function store(Request $request){
        $user_id=$this->auth->user()->id;
        $goods_id=$request->goods_id;
        if(!$goods_id){
            return $this->response()->array([
                'status_code' => 201,
                'message' => '请求参数错误'
            ]);
        }
        //查询购物车中是否已经存在该商品
        $haveCart=Cart::where([['user_id',$user_id],['goods_id',$goods_id],['status',0]])->first();
        if($haveCart){
            return $this->response()->array([
                'status_code' => 201,
                'message' => '购物车中已存在此商品'
            ]);
        }else{
            $cartData=[
                'user_id'=>$user_id,
                'goods_id'=>$goods_id,
                'contact_user_id'=>$request->contact_user_id,
                'selected'=>1,//默认中
                'goods_num'=>1
            ];
            if(Cart::create($cartData)){
                return $this->response()->array([
                    'status_code' => 200,
                    'message' => '成功添加到购物车'
                ]);
            }else{
                return $this->response()->array([
                    'status_code' => 201,
                    'message' => '添加失败'
                ]);
            }
        }


    }
    //更新选中状态
    public function updateSelected(Request $request){
        $carts=$request->all();//获取数组
        if(count($carts)==0){
            return $this->response()->array([
                'status_code' => 202,
                'message' => '请求参数错误'
            ]);
        }
        //检查是否一个都没有选中
        $selected=0;
        foreach ($carts as $v){
            if($v['selected']){
                $selected=1;
            }
        }
        if(!$selected){
            return $this->response()->array([
                'status_code' => 201,
                'message' => '没有选中的商品'
            ]);
        }
        //开启事物
        DB::transaction(function () use($carts) {
            foreach ($carts as $v){
                Cart::where('id',$v['id'])->update(['selected'=>$v['selected']]);
            }
        });
        return $this->response()->array([
            'status_code' => 200,
            'message' => '结算成功'
        ]);


    }

    //删除购物车商品
    public function destroy(Request $request)
    {
        $cartList=$request->all();
        if(!$cartList){
            return $this->response->array([
                'status_code'=>201,
                'message'=>'请求参数错误'
            ]);
        }
        foreach ($cartList as $v){
            if($v['selected']==1){
                Cart::where('id',$v['id'])->delete();
            }
        }
        return $this->response->array([
            'status_code'=>200,
            'message'=>'删除成功'
        ]);
    }

}